package com.vincent.jvmtest.util;

import com.github.rholder.retry.*;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@Slf4j
public class RetryTest {
    public static void main(String[] args) throws ExecutionException, RetryException {
        Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
                .retryIfException()
                // 中间等待一秒
                .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
                // 重试三次
                .withStopStrategy(StopStrategies.stopAfterAttempt(3))
                .build();
        try {
            Boolean call = retryer.call(() -> {
                TimeUnit.SECONDS.sleep(1);
                System.out.println("模拟请求-----------");
                throw new RuntimeException("嘿嘿");
            });
        } catch (Exception e){
            log.error("执行失败",e);
        }
    }
}
